﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrokerBase.Lib.Core;
using BrokerBase.Lib.Policy;
using NHibernate;
using BrokerBase.Lib.Tool;
using BrokerBase.Lib.Product;

namespace BrokerBase.Lib.Service
{
    public class SchemeService:BaseService<IScheme>, ISchemeService
    {

        public SchemeService(ISession session)
            : base(session)
        {

        }

        /// <summary>
        /// 处理业务逻辑,保存保险方案
        /// </summary>
        /// <param name="obj"></param>
        public override void SaveOrUpdate(IScheme obj)
        {

            obj.CalcPayPeriods();

            //重写保险期限、付款期限描述。
            foreach (IPolicyLight pl in obj.PolicyLights)
            {
                pl.InsurancePeriodDescription = PolicyUtil.GetInsurancePeriodDescription(pl);
                pl.PaymentPeriodDescription = PolicyUtil.GetPaymentPeriodDescription(pl);

                if (pl.Policy != null)
                {
                    pl.ProductName = pl.Policy.Product.Name;
                    //pl.ProductType = pl.Policy.Product.ProductType;

                    if (pl.Policy.SelectedInsurancePeriod.Style == InsurancePeriodStyle.Age || pl.Policy.SelectedInsurancePeriod.Style == InsurancePeriodStyle.Period)
                    {
                        pl.InsurancePeriodStyle = pl.Policy.SelectedInsurancePeriod.Style;
                        pl.InsurancePeriodValue = pl.Policy.SelectedInsurancePeriod.Value * pl.Policy.OverInsurancePeriod;
                    }
                    else
                    {
                        pl.InsurancePeriodStyle = InsurancePeriodStyle.Period;
                        pl.InsurancePeriodValue = pl.Policy.InsurancePeriodLength;
                    }

                    if (pl.Policy.SelectedPaymentPeriod.Style == PaymentPeriodStyle.Age || pl.Policy.SelectedPaymentPeriod.Style == PaymentPeriodStyle.Period)
                    {
                        pl.PaymentPeriodStyle = pl.Policy.SelectedPaymentPeriod.Style;
                        pl.PaymentPeriodValue = pl.Policy.SelectedPaymentPeriod.Value * pl.Policy.OverPaymentPeriod;
                    }
                    else
                    {
                        pl.PaymentPeriodStyle = PaymentPeriodStyle.Period;
                        pl.PaymentPeriodValue = pl.Policy.PaymentPeriodLength;
                    }

                    
                    pl.SelectedTimeUnit = pl.Policy.SelectedTimeUnit;
                    pl.UnitType = pl.Policy.Product.UnitType;
                    pl.Amount = pl.Policy.Amount;
                    pl.Fee = pl.Policy.Fee;
                    //pl.TotalFee = pl.Policy.TotalFee;
                }

                pl.TotalFee = pl.PaymentPeriodLength * pl.Fee;
            }


            base.SaveOrUpdate(obj);
        }
    }
}
